package com.tryedhp.signtool;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.AppUtils;
import com.tryedhp.signtool.myapplication.R;

import java.util.ArrayList;
import java.util.List;

import me.zhouzhuo.zzletterssidebar.ZzLetterSideBar;
import me.zhouzhuo.zzletterssidebar.interf.OnLetterTouchListener;
import me.zhouzhuo.zzletterssidebar.widget.ZzRecyclerView;

/**
 * @author tryedhp
 * @date 2019/6/11 15:31
 * @E-mail try2017yx@163.com
 */
public class RecyclerViewActivity extends AppCompatActivity {

    private ZzRecyclerView rv;
    private List<AppInfoModel> mDatas;
    private PersonRecyclerViewAdapter adapter;
    private ZzLetterSideBar sideBar;
    private TextView tvDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);

        setTitle("点击复制");

        tvDialog = (TextView) findViewById(R.id.tv_dialog);
        sideBar = (ZzLetterSideBar) findViewById(R.id.sidebar);
        rv = (ZzRecyclerView) findViewById(R.id.rv);

        mDatas = new ArrayList<>();
        adapter = new PersonRecyclerViewAdapter(this, mDatas);
        rv.setAdapter(adapter);
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<AppInfoModel> personEntities = new ArrayList<>();
                for (AppUtils.AppInfo info : AppUtils.getAppsInfo()) {
                    AppInfoModel appInfo = new AppInfoModel(info.getPackageName(), info.getName(), info.getIcon(), info.getPackagePath(), info.getVersionName(), info.getVersionCode(), info.isSystem());
                    appInfo.setMD5(AppUtils.getAppSignatureMD5(appInfo.getPackageName()));
                    appInfo.setSHA1(AppUtils.getAppSignatureSHA1(appInfo.getPackageName()));
                    appInfo.setSHA256(AppUtils.getAppSignatureSHA256(appInfo.getPackageName()));
                    personEntities.add(appInfo);
                }
                mDatas = personEntities;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (!isFinishing()) {
                            findViewById(R.id.loding).setVisibility(View.GONE);

                            adapter.updateRecyclerView(mDatas);
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
            }
        }).start();

        //set touch event, must add
        sideBar.setLetterTouchListener(rv, adapter, tvDialog, new OnLetterTouchListener() {
            @Override
            public void onLetterTouch(String letter, int position) {
            }

            @Override
            public void onActionUp() {
            }
        });

    }
}
